package cn.jiyun.controller;
import cn.jiyun.pojo.Constactor;
import cn.jiyun.pojo.Developer;
import cn.jiyun.pojo.Project;
import cn.jiyun.result.PageResult;
import cn.jiyun.result.QueryPageBean;
import cn.jiyun.result.Result;
import cn.jiyun.service.ProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("project")
public class ProjectController {

    @Autowired
    private ProjectService projectService;

    @RequestMapping("findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        List<Map> list=projectService.findPage(queryPageBean);
        /*分页查询*/
        PageResult result = new PageResult();
        result.setRows(list);
        /*查询记录数*/
        long i=projectService.jilu();
        result.setTotal(i);
        return result;
    }

    @RequestMapping("add")
    public Result add(@RequestBody Project project){
        int i=projectService.add(project);
        if (i>0){
            return new Result(true,"success");
        }else {
            return new Result(false,"error");
        }
    }

    @RequestMapping("init")
    public List<Developer> init(){
        return projectService.init();
    }

    @RequestMapping("init2")
    public List<Constactor> init2(){
        return projectService.init2();
    }

    @RequestMapping("init3")
    public List<Project> init3(){
        return projectService.init3();
    }

    @RequestMapping("depts")
    public List<Project> depts(){
        return projectService.depts();
    }

    @RequestMapping("edit")
    public Result edit(@RequestBody Project project){
        int i=projectService.edit(project);
        if (i>0){
            return new Result(true,"success");
        }else {
            return new Result(false,"error");
        }
    }

    @RequestMapping("delete")
    public Result delete(Integer pid){
        try {
            projectService.delete(pid);
            return new Result(true,"success");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"error");
        }
    }

    @RequestMapping("findById")
    public List<Project> findById(Integer pid){
        return projectService.findById(pid);
    }
}
